package com.junwell.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.junwell.common.dto.PageDto;
import com.junwell.common.lang.Result;
import com.junwell.entity.Orders;
import com.junwell.entity.Station;
import com.junwell.entity.User;
import com.junwell.service.OrdersService;
import com.junwell.service.StationService;
import com.junwell.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.*;
import redis.clients.jedis.params.GeoRadiusParam;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private StationService stationService;

    @Autowired
    private JedisPool jedisPool;

    @GetMapping("{id}")
    public Result view(@PathVariable("id")Integer id){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        User user = userService.getOne(queryWrapper);
        return Result.succ(200,"成功",user);
    }

    //实体校验
    @PostMapping("/save")
    public Result save(@Validated @RequestBody User user){
        return Result.succ(user);
    }




    @GetMapping("findPage")
    public IPage<User> findPage(PageDto pageDto){
        IPage<User> page = new Page<>(pageDto.getCurrentPage(), pageDto.getPageSize());
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name"  , pageDto.getQueryString());
        page  = userService.page(page, queryWrapper);
        return page;
    }

    @PostMapping("update")
    public Result update(@RequestBody User user){
        boolean flag= userService.updateById(user);
        if (flag){
            return Result.succ("更新成功");
        }else {
            return Result.fail("更新失败");
        }
    }

        /*-------------------------用户端------------------------*/

    @GetMapping("/me/{loginName}")
    public Result viewOne(@PathVariable("loginName")String loginName){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("login_name",loginName);
        User user = userService.getOne(queryWrapper);
        return Result.succ(user);
    }

    @GetMapping("package")
    public IPage<Orders> pack(PageDto pageDto){
        IPage<Orders> page =new Page<>();
        QueryWrapper<Orders> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",pageDto.getQueryString()).or().eq("user_name",pageDto.getQueryString());
        page=ordersService.page(page,queryWrapper);
        return page;
    }

    //根据用户的位置定位，搜索附近的站点位置
    @GetMapping("station")
    public IPage<Station> car(PageDto pageDto,Double longitude,Double latitude,String key){
        IPage<Station> page=new Page<>(pageDto.getCurrentPage(), pageDto.getPageSize());
        QueryWrapper<Station > queryWrapper=new QueryWrapper<>();
        queryWrapper.like("station_name","");
        String queryString = pageDto.getQueryString();
        double radius=Double.parseDouble(queryString);
        page=stationService.page(page,queryWrapper);
        Jedis jedis = jedisPool.getResource();
        System.out.println(jedis.zrem("station", key));
        System.out.println(jedis.geoadd("station", longitude, latitude, key));
        /*jedis.geoadd("china:city",jd,wd,"car");*/
        System.out.println("用户定位"+jedis.geopos("station", key));
        List<GeoCoordinate> user = jedis.geopos("station", key);
        double l1 = user.get(0).getLatitude();
        System.out.println(l1);
        double l2 = user.get(0).getLongitude();
        System.out.println(l2);
        List<GeoRadiusResponse> list = jedis.georadius("station", longitude, latitude,radius, GeoUnit.KM, GeoRadiusParam.geoRadiusParam().withDist());
        for (int i=0;i<page.getRecords().size();i++){
            for (int j = 0; j<list.size(); j++) {
                if (page.getRecords().get(i).getStationName().equals(list.get(j).getMemberByString())) {
                    page.getRecords().get(i).setDistance(list.get(j).getDistance());
                }
            }
        }
        jedis.close();
        return page;

    }

    //查找目的站点的经纬度

    @GetMapping("/take/{station}")
    public  List<GeoCoordinate> take(@PathVariable  String station){
        Jedis jedis = jedisPool.getResource();
        List<GeoCoordinate> data = jedis.geopos("station", station);
        return data;
    }

    @PostMapping("/over/{id}")
    public Result over(@PathVariable Integer id){
        boolean over = ordersService.over(id);
        if (over){
            return  Result.succ("成功");
        }else {
            return Result.fail("失败");
        }
    }


}
